﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HashTable
{
    public class Element<TValue> where TValue : class
    {
        public TValue[] Bucket { get; set; }

        public Element(int size = 2)
        {
            Bucket = new TValue[size];
            for (int i = 0; i < Bucket.Count(); i++)
            {
                Bucket[i] = null;
            }
        }

        public void Add(TValue value)
        {
            int index = GetFirstFree();
            Bucket[index] = value;
        }

        public string Print()
        {
            string s = "";
            for (int i = 0; i < Bucket.Count(); i++)
            {
                if (Bucket[i] != null)
                    s += Bucket[i].ToString() + ", ";
            }
            return s;
        }

        private int GetFirstFree()
        {
            int i = -1;
            for (int j = 0; j < Bucket.Count(); j++)
            {
                if (Bucket[j] == null)
                {
                    i = j;
                    break;
                }
            }
            if (i == -1)
                i = IncreaseBucket();
            return i;
        }

        private int IncreaseBucket(int increase = 2)
        {
            int i = Bucket.Count();
            TValue[] temp = new TValue[Bucket.Count() + increase];
            for (int j = 0; j < Bucket.Count(); j++)
                temp[j] = Bucket[j];
            Bucket = temp;
            return i;
        }
    }
}
